﻿using AutoStar.Common;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace AutoStar.ViewModel
{
    public class CurrentProjectVM : BaseViewModel
    {
        public CurrentProjectVM()
        {
            ModelList = new ObservableCollection<string>();
        }
        public void ModifyModel()
        {
            if (Global.printProject.model.path == "")
            {
                System.Windows.MessageBox.Show("请先导入模型");
                return;
            }
            Global.printProject.model.moveX = float.Parse(XMove);
            Global.printProject.model.moveY = float.Parse(YMove);
            Global.printProject.model.moveZ = float.Parse(ZMove);
            Global.printProject.model.multiple = float.Parse(ModelMult);
            Utilities.ReadSTLFile(Global.printProject.model.path, Global.printProject.model);
        }
        private void OnSelectedModelChanged()
        {
            if (SelectedModel >= 0 && SelectedModel < ModelList.Count)
            {
                string selectedModelName = ModelList[SelectedModel];
                string filePath = System.IO.Path.Combine(GetModelsDirectory(), selectedModelName + ".stl");
                if (File.Exists(filePath))
                {
                    Global.printProject.model = new Model3D
                    {
                        path = filePath
                    };
                    Utilities.ReadSTLFile(filePath, Global.printProject.model);
                }
            }
            Global.MainVM.flushProjectLabel();
        }
        private string GetModelsDirectory()
        {
            return System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)
                   + System.IO.Path.DirectorySeparatorChar.ToString();
        }
        private ObservableCollection<string> _ModelList;
        public ObservableCollection<string> ModelList
        {
            get => _ModelList;
            set => Set(ref _ModelList, value); 
        }

        private int _SelectedModel;
        public int SelectedModel
        {
            get => _SelectedModel;
            set
            {
                Set(ref _SelectedModel, value);
                OnSelectedModelChanged();
            }
        }
        public string _projectName = "（新建项目）";
        public string ProjectName
        {
            get => _projectName;
            set => Set(ref _projectName, value);
        }
        private string _ProjectNum = Global.printProject.id;
        public string ProjectNum
        {
            get => _ProjectNum;
            set => Set(ref _ProjectNum, value);
        }
        private string _createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        public string CreateTime
        {
            get => _createTime;
            set => Set(ref _createTime, value);
        }
        private string _updateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        public string UpdateTime
        {
            get => _updateTime;
            set => Set(ref _updateTime, value);
        }
        private string _ProjectRemark;
        public string ProjectRemark
        {
            get => _ProjectRemark;
            set => Set(ref _ProjectRemark, value);
        }
        private string _TotalLayers = "0";
        public string TotalLayers
        {
            get => _TotalLayers;
            set => Set(ref _TotalLayers, value);
        }
        private string _PrintTime = "60";
        public string PrintTime
        {
            get => _PrintTime + "s";
            set => Set(ref _PrintTime, value);
        }
        private string _ModelMult = "60";
        public string ModelMult
        {
            get => _ModelMult;
            set => Set(ref _ModelMult, value);
        }
        private string _XMove = "0";
        public string XMove
        {
            get => _XMove;
            set => Set(ref _XMove, value);
        }
        private string _YMove = "0";
        public string YMove
        {
            get => _YMove;
            set => Set(ref _YMove, value);
        }
        private string _ZMove = "0";
        public string ZMove
        {
            get => _ZMove;
            set => Set(ref _ZMove, value);
        }
    }
}
